package redis;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;








import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;



import org.json.simple.parser.JSONParser;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.Tuple;

public class test {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		String redisHost = "";
		
		redisHost = "rp-1";
		redisHost = "172.18.38.14";
/**
		String connections = "test.connections";
		String ll = "test.mylist";
		
		Jedis jedis = new Jedis(redisHost); 
		//jedis.set("test.foo", "Craig is great");
		//jedis.set(connections,"0");
		//String value = jedis.get("foo");
		//jedis.expire("foo", 4);
		
		//jedis.lpush(ll, "craig");
		//jedis.lpush(ll, "david");
		
	//	List<String> slist = jedis.lrange(ll, 0, -1);
		
		//long i = jedis.llen(ll);
		
		//System.out.println(i);
		
 		
    //    for(int i=0; i<jedis.llen(ll); i++){
    //        System.out.println(jedis.lrange(ll, i, i));
    //   }
		
		//System.out.println(slist.contains("craig"));
		//System.out.println(slist);
		
		
		
		System.out.println("Updating REDIS");
		
		jedis.incr(connections);
		System.out.println(jedis.get(connections));
		jedis.incr(connections);
		System.out.println(jedis.get(connections));
		jedis.incr(connections);
		System.out.println(jedis.get(connections));
		//System.out.println(jedis.get("foo"));
		System.out.println("Finished REDIS");
		jedis.disconnect();
		
	**/	
		Jedis jedis = new Jedis(redisHost); 
		
		
		JSONObject obj = new JSONObject();
		obj.put("name", "herohead.com");
		obj.put("email", "craig.david@mt.com");
		obj.put("age", new Integer(100));
	 
		JSONArray list = new JSONArray();
		list.add("msg 1");
		list.add("msg 2");
		list.add("msg 3");
	 
		obj.put("messages", list);

		System.out.println(obj.toJSONString());
	
		jedis.hset("program", "json", obj.toJSONString());
		
		String stemp = jedis.hget("program", "json");
		
		
		JSONParser parser = new JSONParser();
		
		
		
		Object obj1 = parser.parse(jedis.hget("program", "json"));
		
		JSONObject jsonObject = (JSONObject) obj1;
		
		System.out.println(jsonObject.size());
		System.out.println(jsonObject.containsValue("craig.david@mt.com"));
		System.out.println(jsonObject.values());
		System.out.println(jsonObject.keySet());
		
		
		String name = (String) jsonObject.get("name");
		System.out.println(name);
		
		JSONArray msg = (JSONArray) jsonObject.get("messages");
		Iterator<String> iterator = msg.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
			
		}
		
		
		Set keys =  jsonObject.keySet();
		
		Iterator<String> iterator1 = keys.iterator();
		while (iterator1.hasNext()) {
			//System.out.println(iterator1.next());
			name = (String) jsonObject.get(iterator1.next());
			System.out.println(name);
			//System.out.println(jsonObject.get(iterator1.next()));
			
		}
		
		
		/**
		
		long start = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
		    String result = jedis.set("" + i, "" + i);
		}
		long end = System.currentTimeMillis();
		System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds");

		**/
				
		String failKey = "prod.nw.server.failures";
		String failGroupKey = "prod.nw.group.failures";
		
		
		String keyServerGood = "prod.nw.server.good";
		
		//Set<String> l = jedis.zrevrange(keyServerGood, 0, 10);
		//Set<String> l = jedis.zrevrange(keyServerGood, 0, 10);
		//Set<Tuple> l = jedis.zrevrangeWithScores(keyServerGood, 0, 9);
		
		// danger below *****
		
		/**
		Set<String> skeys = jedis.keys("t:*");
		for (String s : skeys) {
		    System.out.println(s);
		    jedis.del(s);
		}
		**/
		// danger above *******
		
		System.exit(0);
		
		jedis.sadd("test:backup-alerts:nw:eu:server#ch00sa191", "filesystem");
		jedis.expire("test:backup-alerts:nw:eu:server#ch00sa191",2000);
		
		//System.out.println(s1);
		
		String s2 = jedis.get("prod:backup-alerts:nw:eu:server#ch00sa191");
		if (s2 == null) {
			System.out.println("not found");
		} 
		
		
		//List<String> messages = Arrays.asList("server:ch00sa191", "module:mssql");
		//List<String>  messages = Arrays.asList(s2.split("\\s*,\\s*"));
		//List<String> messages = Arrays.asList(s2);
		
		//System.out.println(messages.contains("module:mssql"));
		
		//for (String s : messages) {
		//    System.out.println(s);
		//}
		
		
		
		//System.out.println(s2.split(",")[1].split(":")[1]);
		//System.out.println(s2);
		
		
		//System.out.println(l.size());
		//l.toString()
		
		
		//Integer intVal = (Integer) l.getValue(1);
		
		//System.out.println(l.toString());
		
		/**
		
		for (Tuple s : l) {
		    System.out.println(s.getScore());
		    System.out.println(s.getElement());
		}
**/
		//System.out.println(l);
		
		/**
		
		long start = System.currentTimeMillis();
		Transaction tx = jedis.multi();
		
		tx.zincrby(failKey, 1, "ch00sm33");
		tx.zincrby(failKey, 1, "ch00sm33");
		tx.zincrby(failKey, 1, "ch00spb1q");
		tx.zincrby(failKey, 1, "ch00spb1");
		tx.zincrby(failKey, 1, "ch00spb1");
		tx.zincrby(failKey, 1, "ch00spb1");
		tx.zincrby(failKey, 1, "ch00spb1q");
		tx.zincrby(failKey, 1, "ch00spb1");
		tx.zincrby(failGroupKey, 1, "testgroup");
		
		
		
		
		
		//for (int i = 0; i < 100000; i++) {
		//    tx.set("" + i, "" + i);
	//	}
		List<Object> results = tx.exec();
		long end = System.currentTimeMillis();
		System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");

		
		**/
		
		
	
	
	}

}
